//
//  LTChooseUnitViewController.m
//  LearnTeacher
//
//  Created by 高刘通 on 2024/6/7.
//

#import "LTChooseUnitViewController.h"
#import "LTChooseUnitView.h"
#import "LTChooseUnitModel.h"
#import "LTWebViewController.h"
#import "LTChooseContentLeftModel.h"
#import "NSObject+YYModel.h"
#import "LTHomeworkAipManager.h"

@interface LTChooseUnitViewController ()
@property(strong, nonatomic) LTChooseUnitView *rootView;
@end

@implementation LTChooseUnitViewController

- (void)viewDidLoad {
    if (!self.showColor) {
        self.backgourndColor = [UIColor clearColor];
    }
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.contentView addSubview:self.rootView];
    self.titleLabel.text = @"选择单元";
    self.rootView.dataSource = self.dataSource;
}

- (void)setDataSource:(NSArray *)dataSource {
    _dataSource = dataSource;
}

- (void)switchunit:(NSString *)unitid name:(NSString *)name {
    __weak typeof(self) weakSelf = self;
    [SVProgressHUD show];
    [LTHomeworkAipManager switchunitWithParam:@{@"unitid":unitid, @"versionId":self.versionId} result:^(NSDictionary * _Nonnull dic) {
        [SVProgressHUD dismiss];
        [[NSNotificationCenter defaultCenter] postNotificationName:ChooseUnitSuccess object:name userInfo:@{@"content":weakSelf.name.length ? weakSelf.name :@"", @"id":[weakSelf.versionId integerValue] > 0 ? weakSelf.versionId : @""}];
        [weakSelf dismiss];
    }];
}

- (LTChooseUnitView *)rootView {
    if (!_rootView) {
        _rootView = [[LTChooseUnitView alloc] initWithFrame:self.contentView.bounds];
        __weak typeof(self) weakSelf = self;
        [_rootView setDidSelectHandler:^(LTChooseUnitModel *model) {
            [weakSelf switchunit:model.unitid name:model.name];
        }];
    }
    return _rootView;
}

- (void)dealloc {
    NSLog(@"dealloc");
}

@end

